package com.orhon.smartcampus.modules.student.fetcher;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.basicComponents.service.DateService;
import com.orhon.smartcampus.basicComponents.service.GQLUtil;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPeriod;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.PeriodMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.ArrivesDao;
import com.orhon.smartcampus.modules.student.dao.EclassHeadteacherDao;
import com.orhon.smartcampus.modules.student.dao.EclassRecordsDao;
import com.orhon.smartcampus.modules.student.dao.SInformationDao;
import com.orhon.smartcampus.modules.student.entity.*;
import com.orhon.smartcampus.modules.student.gqlModel.*;
import com.orhon.smartcampus.modules.student.mm.*;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import graphql.schema.DataFetcher;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;


@Component
public class EclassRecordssFetcher implements DataFetcher {
	@Autowired
	EclassHeadteacherDao eclassHeadteacherDao;
	@Autowired
    EclassRecordsDao eclassRecordsDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
        List<GQLEclassRecords> GQLEclassRecords = new ArrayList<>();
        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
        String hql = "from EclassRecords where deleted_at = null ";
        if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
            if(map.get("user_id")!=null) {
                hql += "and user_id" + Util.decide(map.get("user_id"));
            }
            if(map.get("original_eclass")!=null) {
                hql += "and original_eclass " + Util.decide(map.get("original_eclass"));
            }
            if(map.get("go_eclass")!=null) {
                hql += "and go_eclass " + Util.decide(map.get("go_eclass"));
            }
            if(map.get("transaction_type")!=null) {
                hql += "and transactionType " + Util.decide(map.get("transaction_type"));
            }
            if(map.get("status")!=null) {
                hql += "and status " + Util.decide(map.get("status"));
            }

        }
        List<EclassRecords> EclassRecordsList = eclassRecordsDao.find(hql, null, page.getPage(), page.getLimit());
        for(EclassRecords item:EclassRecordsList){
            GQLStudent gqlStudent =null;
            if(env.getSelectionSet().contains("data/student")){
                SInformation student = item.getStudent();
                gqlStudent = StudentMapper.MAPPER.toStudent(student);
            }
            GQLUser gqlUser =null;
            if(env.getSelectionSet().contains("data/users")){
                Users users = item.getUsers();
                gqlUser = UserMapper.MAPPER.mapUser(users);
                System.out.println("sdfasdjfkasjdlfjklasdjfla;sjdkfjal;sdjfklajskdlfasdfa"+"users="+gqlUser);
            }
            GQLEclass gqlEclass =null;
            if(env.getSelectionSet().contains("data/originalEclass")){
                Eclass eclass = item.getOriginalEclass();
                gqlEclass = EclassMapper.MAPPER.toEclass(eclass);
            }
            GQLEclass gqlEclass1 =null;
            if(env.getSelectionSet().contains("data/goEclass")){
                Eclass goEclass = item.getGoEclass();
                gqlEclass1 = EclassMapper.MAPPER.toEclass(goEclass);
            }

            GQLEclassRecords gqlEclassRecords = EclassRecordssMapper.MAPPER.toEclassRecords(item);
            gqlEclassRecords.setUsers(gqlUser);
            gqlEclassRecords.setStudent(gqlStudent);
            gqlEclassRecords.setGoEclass(gqlEclass1);
            gqlEclassRecords.setOriginalEclass(gqlEclass);
            GQLEclassRecords.add(gqlEclassRecords);
            System.out.println("iiiii="+gqlEclassRecords);
        }
        Long total = 0L;
        if (env.getSelectionSet().contains("total")){
            total = eclassRecordsDao.getTotalCount(hql);
        }
        return Result.ok().put("data", GQLEclassRecords).put("total", total);
    }
/*
		List<GQLHeadTeacher> GQLHeadTeacher = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from EclassHeadteacher where deleted_at = null ";
		List<EclassHeadteacher> list = eclassHeadteacherDao.find(hql,null,page.getPage(),page.getLimit());

		for (EclassHeadteacher item : list) {
			GQLEclass gqlEclass = null;
			if (env.getSelectionSet().contains("eclass")){
				Eclass eclass = item.getStudentEclassByEclassId();
				gqlEclass = EclassMapper.MAPPER.toEclass(eclass);
			}

		GQLTeacher gqlTeacher = null;
			if (env.getSelectionSet().contains("teacher")){
				TInformation tInformation = item.getTeacherTInformationByTeacherId();
				gqlTeacher = TeacherMapper.MAPPER.toTeacher(tInformation);
			}

			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}*/

//			GQLHeadTeacher teacher = EclassHeadteacherMapper.MAPPER.mapEntity(item);
//			teacher.setEclass(gqlEclass);
//			teacher.setSchool(gqlSchool);
//			teacher.setTeacher(gqlTeacher);
//			GQLHeadTeacher.add(teacher);
//
//		}
//
//		Long total = 0L;
//		if (env.getSelectionSet().contains("total")){
//			total = eclassHeadteacherDao.getTotalCount("Eclass");
//		}
//		return Result.ok().put("data", GQLHeadTeacher).put("total", total);






}
